﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Foothill.WF.Command;
using Foothill.Utils;
using Foothill.WF.Core;
using GGFW.Utils.Constants;
using GGFW.Entity.DocumentsManage;
using GGFW.BusinessLogic.DocumentsManage;

namespace GGFW.Command.DocumentsManage
{
    public class OrdersCommand : DispatchCommand
    {
        /// <summary>
        /// 订单信息业务逻辑访问类
        /// </summary>
        private readonly OrdersControl OrderCtrl = new OrdersControl();

        #region 获取订单数据(v_orders)
        /// <summary>
        /// 获取订单数据
        /// </summary>
        /// <param name="context">HttpContext</param>
        /// <param name="requestType">RequestType</param>
        /// <param name="url">url</param>
        /// <returns>返回订单信息数据</returns>
        public ResultBase onGetViewOrders(HttpContext context, RequestType requestType, string url)
        {
            //返回列表集合
            ResultBase result = null;
            try
            {
                //将查询条件封装
                v_orders v_ordersEntity = GetFormEntity<v_orders>();
                result = SearchViewOrders(v_ordersEntity, context);
            }
            catch (Exception ex)
            {
                result = new ResultBase();
                result.Success = false;
                result.Message = ex.Message;
                result.AddErrorDetail((int)GGFW.Utils.Constants.ErrorCode.ERROR_EXCPETION, ex.Message);
                log.Fatal(ex.Message);
            }
            return result;
        }

        /// <summary>
        /// 获取查询条件的方法
        /// </summary>
        /// <param name="queryEntity">查询条件</param>
        /// <param name="context">请求</param>
        /// <returns></returns>
        public FoothillPageData<v_orders> SearchViewOrders(v_orders v_ordersEntity, HttpContext context)
        {
            string start = context.Request["start"];
            string limit = context.Request["limit"];
            v_ordersEntity.OrderField = context.Request["sort"];
            v_ordersEntity.OrderDirection = context.Request["dir"];
            int intStart = NumericUtil.toInteger(start, 0);
            int intLimit = NumericUtil.toInteger(limit, 1);
            if (intLimit == 0) intLimit = 1;
            int pageNo = intStart / intLimit + 1;
            int pageSize = intLimit;
            return OrderCtrl.SearchViewOrders(pageSize, pageNo, v_ordersEntity);
        }
        #endregion

        #region 保存订单数据
        /// <summary>
        /// 保存订单数据
        /// </summary>
        /// <param name="context"></param>
        /// <param name="requestType"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public ResultBase onSaveOrder(HttpContext context, RequestType requestType, string url)
        {

            //返回列表集合
            ResultBase result = new ResultBase();
            try
            {
                int order_id = 0;
                string orderFileds = context.Request["orderFileds"].Replace('\'', '’').Replace('ψ', '\'');
                string ordergridFileds = context.Request["ordergridFileds"].Replace('\'', '’').Replace('ψ', '\'');

                if (!string.IsNullOrEmpty(orderFileds))
                    order_id = OrderCtrl.SaveOrder(orderFileds);

                //新增
                if (order_id != 0)
                {
                    if (!string.IsNullOrEmpty(ordergridFileds))
                    {
                        OrderCtrl.SaveOrderGrid(ordergridFileds, order_id);
                    }
                }

                result.Success = true;

            }
            catch (Exception ex)
            {
                result = new ResultBase();
                result.Success = false;
                result.Message = ex.Message;
                result.AddErrorDetail((int)GGFW.Utils.Constants.ErrorCode.ERROR_EXCPETION, ex.Message);
                log.Fatal(ex.Message);
            }
            return result;
        }
        #endregion

        #region 获取订单详细信息(v_ordersinfo)
        /// <summary>
        /// 获取订单详细信息(v_ordersinfo)
        /// </summary>
        /// <param name="context">HttpContext</param>
        /// <param name="requestType">RequestType</param>
        /// <param name="url">url</param>
        /// <returns>返回订单详细信息</returns>
        public ResultBase onGetViewOrdersInfo(HttpContext context, RequestType requestType, string url)
        {
            //返回列表集合
            ResultBase result = null;
            try
            {
                //将查询条件封装
                v_ordersinfo v_ordersinfoEntity = GetFormEntity<v_ordersinfo>();
                result = SearchViewOrdersInfo(v_ordersinfoEntity, context);
            }
            catch (Exception ex)
            {
                result = new ResultBase();
                result.Success = false;
                result.Message = ex.Message;
                result.AddErrorDetail((int)GGFW.Utils.Constants.ErrorCode.ERROR_EXCPETION, ex.Message);
                log.Fatal(ex.Message);
            }
            return result;
        }

        /// <summary>
        /// 获取查询条件的方法
        /// </summary>
        /// <param name="queryEntity">查询条件</param>
        /// <param name="context">请求</param>
        /// <returns></returns>
        public FoothillPageData<v_ordersinfo> SearchViewOrdersInfo(v_ordersinfo v_ordersinfoEntity, HttpContext context)
        {
            string start = context.Request["start"];
            string limit = context.Request["limit"];
            v_ordersinfoEntity.OrderField = context.Request["sort"];
            v_ordersinfoEntity.OrderDirection = context.Request["dir"];
            int intStart = NumericUtil.toInteger(start, 0);
            int intLimit = NumericUtil.toInteger(limit, 1);
            if (intLimit == 0) intLimit = 1;
            int pageNo = intStart / intLimit + 1;
            int pageSize = intLimit;
            return OrderCtrl.GetViewOrdersInfo(pageSize, pageNo, v_ordersinfoEntity);
        }
        #endregion

        #region 获取订单生产单数据(v_mo)
        /// <summary>
        /// 获取订单生产单数据
        /// </summary>
        /// <param name="context">HttpContext</param>
        /// <param name="requestType">RequestType</param>
        /// <param name="url">url</param>
        /// <returns>返回订单信息数据</returns>
        public ResultBase onGetViewMO(HttpContext context, RequestType requestType, string url)
        {
            //返回列表集合
            ResultBase result = null;
            try
            {
                //将查询条件封装
                v_mo v_moEntity = GetFormEntity<v_mo>();
                result = SearchViewMO(v_moEntity, context);
            }
            catch (Exception ex)
            {
                result = new ResultBase();
                result.Success = false;
                result.Message = ex.Message;
                result.AddErrorDetail((int)GGFW.Utils.Constants.ErrorCode.ERROR_EXCPETION, ex.Message);
                log.Fatal(ex.Message);
            }
            return result;
        }

        /// <summary>
        /// 获取查询条件的方法
        /// </summary>
        /// <param name="queryEntity">查询条件</param>
        /// <param name="context">请求</param>
        /// <returns></returns>
        public FoothillPageData<v_mo> SearchViewMO(v_mo v_moEntity, HttpContext context)
        {
            string start = context.Request["start"];
            string limit = context.Request["limit"];
            v_moEntity.OrderField = context.Request["sort"];
            v_moEntity.OrderDirection = context.Request["dir"];
            int intStart = NumericUtil.toInteger(start, 0);
            int intLimit = NumericUtil.toInteger(limit, 1);
            if (intLimit == 0) intLimit = 1;
            int pageNo = intStart / intLimit + 1;
            int pageSize = intLimit;
            return OrderCtrl.GetViewMO(pageSize, pageNo, v_moEntity);
        }
        #endregion

        #region 更新订单表(orders)信息
        /// <summary>
        /// 更新订单表(orders)信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="requestType"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public ResultBase onUpdateOrdersInfo(HttpContext context, RequestType requestType, string url)
        {
            //返回列表集合
            ResultBase result = null;
            try
            {
                string order_id = context.Request["OrderID"];
                string tab_name = context.Request["TabName"];
                string filedname = context.Request["FiledName"];
                string filedvalue = context.Request["FiledValue"];
                result = OrderCtrl.UpdateOrdersInfo(order_id, tab_name, filedname, filedvalue);

                result.Success = true;
            }
            catch (Exception ex)
            {
                result = new ResultBase();
                result.Success = false;
                result.Message = ex.Message;
                result.AddErrorDetail((int)GGFW.Utils.Constants.ErrorCode.ERROR_EXCPETION, ex.Message);
                log.Fatal(ex.Message);
            }
            return result;
        }
        #endregion

        #region 更新订单表(ordersgrid)信息
        /// <summary>
        /// 更新订单表(ordersgrid)信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="requestType"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public ResultBase onUpdateOrdersGridfield(HttpContext context, RequestType requestType, string url)
        {
            //返回列表集合
            ResultBase result = null;
            try
            {
                string og_id = context.Request["OgID"];
                string order_id = context.Request["OrderID"];
                string filednmae = context.Request["FieldName"];
                string fieldvalue = context.Request["FieldValue"];

                result = OrderCtrl.UpdateOrdersGridfield(og_id, order_id, filednmae, fieldvalue);

                result.Success = true;
            }
            catch (Exception ex)
            {
                result = new ResultBase();
                result.Success = false;
                result.Message = ex.Message;
                result.AddErrorDetail((int)GGFW.Utils.Constants.ErrorCode.ERROR_EXCPETION, ex.Message);
                log.Fatal(ex.Message);
            }
            return result;
        }
        #endregion

        #region  删除订单
        /// <summary>
        /// 删除订单
        /// </summary>
        /// <param name="context">HttpContext</param>
        /// <param name="requestType">RequestType</param>
        /// <param name="url"></param>
        /// <returns>ResultBase</returns>
        public ResultBase onDelOrdersByID(HttpContext context, RequestType requestType, string url)
        {
            //返回结果
            ResultBase result = null;
            try
            {
                //从提交的表单获得数据实体
                string order_ids = context.Request["order_ids"].ToString();
                foreach (string order_id in order_ids.Split(','))
                {
                    result = OrderCtrl.DelOrdersByID(NumericUtil.toInteger(order_id, 0));
                }
                result.success = true;

            }
            catch (Exception ex)
            {
                result = new ResultBase();
                result.Success = false;
                result.Message = ex.Message;
                result.AddErrorDetail((int)GGFW.Utils.Constants.ErrorCode.ERROR_EXCPETION, ex.Message);
            }
            return result;
        }
        #endregion

    }
}
